package com.gxa.hotel.portal.service.impl;


import com.gxa.hotel.core.pojo.domain.PayDetail;
import com.gxa.hotel.core.pojo.domain.RoomOrder;
import com.gxa.hotel.core.pojo.domain.RoomState;
import com.gxa.hotel.core.pojo.domain.Rooms;
import com.gxa.hotel.core.util.RestUtils;
import com.gxa.hotel.core.util.SnowFlakeUtil;
import com.gxa.hotel.portal.service.RoomsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
public class RoomsServiceImpl implements RoomsService {

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    @Qualifier("myJmsTemplate")
    JmsMessagingTemplate jmsMessagingTemplate=null;

    //  private static final String HOTEL_LIST="hotel:list";

    private static final String HOTEL="hotel:";


    /*
    酒店列表的信息
     */
    @Override
    public List<RoomState> selectList(String[] date) {
        String begin=date[0];
        String end=date[date.length-1] ;

        String key=begin+"-"+end+":list";
        BoundHashOperations hashOperations = redisTemplate.boundHashOps(key);

        List<RoomState> data=null;
        List values = hashOperations.values();

        if(values.size()==0){
            String url="http:localhost:8080/room/list";
            MultiValueMap<String,Object> params= new LinkedMultiValueMap<>();
            params.add("date",date);
            Map map = RestUtils.postWithParams(url,params);
            data= (List<RoomState>) map.get("data");

            for (int i = 0; i <data.size() ; i++) {
                RoomState roomState=data.get(i);
                hashOperations.put(roomState.getRsType(),roomState);
                hashOperations.expire(10,TimeUnit.MINUTES);
            }
        }
        Map entries = hashOperations.entries();
        Set set = entries.entrySet();
        Iterator iterator1 = set.iterator();
        while (iterator1.hasNext()){
            RoomState next = (RoomState) iterator1.next();
            data.add(next);
        }

        List list = new ArrayList<>();

        return data;
    }



    @Override
    public String selectDetail(String name) {
        BoundValueOperations stringOperations = redisTemplate.boundValueOps(name);
        Object o = (Rooms) stringOperations.get();
        //缓存没有就做远程调用
        if(StringUtils.isEmpty(o)){
            String url="http://localhost:8080/room/roomdetail";
            MultiValueMap<String,Object> params= new LinkedMultiValueMap<>();
            params.add("name",name);
            Map map = RestUtils.postWithParams(url,params);
             o =  map.get("data");

             //写入缓存
             stringOperations.set(o,10,TimeUnit.MINUTES);
        }
        return o.toString();
    }

    /**
     * 发送订单给后台做操作
     * @param roomOrder
     */
    @Override
    public void sendorder(RoomOrder roomOrder, PayDetail payDetail) {

        Long orderId = SnowFlakeUtil.getSnowFlakeId();
        BoundHashOperations hashOperations = redisTemplate.boundHashOps(orderId.toString());
        hashOperations.put("order",roomOrder);
        hashOperations.put("pay",payDetail);
        redisTemplate.expire(orderId.toString(),10,TimeUnit.MINUTES);
        jmsMessagingTemplate.convertAndSend("roomsOrder",orderId);

    }
}
